﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;
using System.Globalization;

namespace School.Service.Uniform
{
    public static class SRV_CLOTHES_STOCKTACKINGClass
    {
        private static SchoolEntities db = new SchoolEntities();
       
        public static bool SRV_CLOTHES_STOCKTACKINGInsert(List<SRV_CLOTHES_STOCKTACKING> lstStock)
        {
            foreach (SRV_CLOTHES_STOCKTACKING item in lstStock)
            {
                db.AddToSRV_CLOTHES_STOCKTACKING(item);
            }
          
            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        
        public static IEnumerable<object> SRV_CLOTHES_STOCKTACKINGSearch(DateTime date)
        {
            var query = from stock in db.SRV_CLOTHES_STOCKTACKING
                        where stock.Date_Time == date
                        select new
                        {
                            SizeId = stock.Size_Id,
                            Size_Code = stock.SRV_Clothe_Sizes.Size_Code,
                            Name = stock.SRV_Clothe_Sizes.SRV_Clothe.Name,
                            ClotheCode = stock.SRV_Clothe_Sizes.SRV_Clothe.Code,
                            SizeName = stock.SRV_Clothe_Sizes.Name,
                            SystemQty = stock.System_Qty,
                            Pysical_Qty = stock.Pysical_Qty,
                            Differance = stock.Pysical_Qty - stock.System_Qty,
                            Equating_Date_Time = stock.Equating_Date_Time != null ? (DateTime)stock.Equating_Date_Time : (DateTime?)null
                        };
           return query;
                
        }
       
        public static IEnumerable<SRV_CLOTHES_STOCKTACKING> SRV_CLOTHES_STOCKTACKINGAll()
        {
            IEnumerable<SRV_CLOTHES_STOCKTACKING> query = from stocks in db.SRV_CLOTHES_STOCKTACKING
                        select stocks;
            return query.GroupBy(s=>s.Date_Time).Select(s=>s.FirstOrDefault());
        }
       
        public static bool SizesEquakity(List<string> lstsizeCode,DateTime date)
        {
            CultureInfo SaCulture = new CultureInfo("ar-eg");
            DateTimeFormatInfo hijriDateInfo = SaCulture.DateTimeFormat;
            hijriDateInfo.ShortDatePattern = "MM/dd/yyyy";
            foreach (string sizecode in lstsizeCode)
            {
                db.SRV_Clothe_Sizes.Where(s => s.Size_Code == sizecode).First().QTY =
                    db.SRV_CLOTHES_STOCKTACKING.Where(s => s.SRV_Clothe_Sizes.Size_Code == sizecode &&
                        s.Date_Time == date).First().Pysical_Qty;
                db.SRV_CLOTHES_STOCKTACKING.Where(s => s.SRV_Clothe_Sizes.Size_Code == sizecode &&
                      s.Date_Time == date).First().Equating_Date_Time =Convert.ToDateTime(DateTime.Now.ToShortDateString(),hijriDateInfo);
            }

            try
            {
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
